package com.aotain.oframework.util.ioc.i18n;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.Resource;

/**
 * @author Jacob.cw.Zhang@gmail.com
 */
public class ExtendedMessageSource extends ReloadableResourceBundleMessageSource {
	
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	private final HashSet<Resource> currentSet = new HashSet<Resource>();

	public void addMessageBundles(Collection<Resource> messageBundles) throws IOException {
		getCurrentSet().addAll(messageBundles);
		List<String> strArray = new ArrayList<String>();
		for(Resource res: this.getCurrentSet()){
			log.debug("Adding message bundle : {}", res.getURI());
			strArray.add(res.getURI().toString());
		}
		setBasenames(strArray.toArray(new String[0]));
	}

	/**
	 * @return the value for currentSet
	 */
	public HashSet<Resource> getCurrentSet() {
		return currentSet;
	}

	@Override
	public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
		super.setAlwaysUseMessageFormat(alwaysUseMessageFormat);
	}

	@Override
	protected boolean isAlwaysUseMessageFormat() {
		return super.isAlwaysUseMessageFormat();
	}

	@Override
	protected String renderDefaultMessage(String defaultMessage, Object[] args, Locale locale) {
		return super.renderDefaultMessage(defaultMessage, args, locale);
	}

	@Override
	protected String formatMessage(String msg, Object[] args, Locale locale) {
		return super.formatMessage(msg, args, locale);
	}

	@Override
	protected MessageFormat createMessageFormat(String msg, Locale locale) {
		return super.createMessageFormat(msg, locale);
	}
	
}
